package com.alibaba.it.asset.codegen.db2entity.processor;

import com.alibaba.it.asset.codegen.db2entity.GenerateProcessor;
import com.alibaba.it.asset.codegen.db2entity.PropertyUtil;
import com.alibaba.it.asset.codegen.db2entity.vo.DBColumnVO;
import com.alibaba.it.asset.codegen.db2entity.vo.PrimaryKeyColumnVO;
import com.google.common.collect.Lists;
import org.apache.commons.io.FileUtils;

import java.util.ArrayList;
import java.util.List;


public abstract class GenerateProcessorAbstract implements GenerateProcessor {

    protected static String TPL_PATH = PropertyUtil.getProperty("gen.tpl");

    protected List<DBColumnVO> getNoPrimaryKey(List<DBColumnVO> columns) {
        List<DBColumnVO> cos = new ArrayList<DBColumnVO>();
        for (DBColumnVO column : columns) {
            if (column instanceof PrimaryKeyColumnVO == false) {
                cos.add(column);
            }
        }
        return cos;
    }

    protected boolean isAbstractEntity(List<DBColumnVO> primary, List<DBColumnVO> columns) {
        boolean isAbstractEntity = false;

        if (primary != null && primary.size() == 1) {
            PrimaryKeyColumnVO id = (PrimaryKeyColumnVO) primary.get(0);

          /*  if (id.getColumnName().toUpperCase().equals("ID") && id.isAutoIncrement()) {
                isAbstractEntity = true;
            }*/
          /*  if (id.getColumnName().toUpperCase().equals("ID")) {
                isAbstractEntity = true;
            }*/
            //查询Version字段
//		DBColumnVO version = null;
//		  for(DBColumnVO column:columns){
//			  if(column.getColumnName().toUpperCase().equals("VERSION")){
//				  version = column;
//			  }
//		  }
//		if(version==null || version.getDataType()!=Types.INTEGER){
//			isAbstractEntity = false;
//		}
        }

        return isAbstractEntity;
    }

    protected List<DBColumnVO> getPrimaryKey(List<DBColumnVO> columns) {
        List<DBColumnVO> cos = new ArrayList<DBColumnVO>();
        for (DBColumnVO column : columns) {
            if (column instanceof PrimaryKeyColumnVO) {
                cos.add(column);
            }
        }
        return cos;
    }

    /**
     * AbstractEntity中返回非ID以及非Version的字段
     *
     * @param commons
     * @return
     */
    protected List<DBColumnVO> removeIdAndVersionForColumns(List<DBColumnVO> commons) {
        List<DBColumnVO> news = new ArrayList<DBColumnVO>();
        for (DBColumnVO common : commons) {
            if (!common.getColumnName().toUpperCase().equals("ID") && !common.getColumnName().toUpperCase().equals("VERSION")) {
                news.add(common);
            }
        }
        return news;
    }

    protected String generateJavaClass(String name) {
        String result = name.toLowerCase();
        String[] specialCharacters = new String[]{"-", "_"};
        for (String specialCharacter : specialCharacters) {
            int index = result.indexOf(specialCharacter);
            while (index != -1) {
                if (index == result.length() - 1) {
                    break;
                }
                result = result.substring(0, index) + result.substring(index + 1, index + 2).toUpperCase() + result.substring(index + 2);
                index = result.indexOf(specialCharacter);
            }
        }
        result = result.replaceAll("[^a-zA-Z]", "");
        result = result.substring(0, 1).toUpperCase() + result.substring(1);
        return result;
    }

    protected boolean isCanCreate(String path, boolean overwrite) {
        if (!overwrite && FileUtils.getFile(path).exists()) {
            System.out.println(path + " is exists");
            return false;
        }
        return true;
    }
}
